

local CustomClock = CustomClock;


AUTOBGR_BG_NAMES           = {
["Alterac Valley"]         = true,
["Eye of the Storm"]       = true,
["Arathi Basin"]           = true,
["Warsong Gulch"]          = true,
}

do
	local ccFrame = CustomClock_DisplayFrame;
	
	ccFrame:RegisterEvent("VARIABLES_LOADED");
	ccFrame:RegisterEvent("PLAYER_LOGOUT");
	ccFrame:RegisterEvent("PLAYER_DEAD")
	
	ccFrame.TEXT = CustomClock_DisplayFrameText;
	ccFrame.CUSTOMCLOCK = CustomClock;
	CustomClock.DISPLAY_FRAME = ccFrame;
	
	ccFrame.Elapsed = 0.0;
	CustomClock.SCRIPTS_FRAME = ccFrame;
	
	
	
end


function padOutHex(IN)

   x = string.len(IN);
   y = 6-x;
   return "[cFF"..string.rep("0",y)..IN.."!";
   
end

function decToFloat(IN)
  return IN / 255
end

function facingAngle()
local facing
math.deg(select(49,Minimap:GetChildren()):GetFacing())
if GetCVar("rotateMinimap") == "0" then
    facing = math.deg(select(49,Minimap:GetChildren()):GetFacing() )
else
    facing = math.deg(MiniMapCompassRing:GetFacing() )
end
return facing
end

function CustomClock:GetPosInfo()
  local ReferenceMark = "|cFFFFCC33!"; 
  local ReferenceMark2 = "|cFFFFCC22!"; 
  local ReferenceMark3 = "|cFFFFCC11!";
  local ReferenceMark4 = "|cFFFFCC05!";
  local MarkEnd = "|cFFF1CC01!";
  local nullReference = "|cFF660000!";

  local subzoneText = "";
  local outstr = "|cFFFF0000This!|r";
  local inBattleGround = nullReference;
  local inZone = nullReference;
  local isOutDoors = nullReference;
  
  local isDead = nullReference;
  local inSubZone = nullReference;
  local inQueue = nullReference;
  local coordsX = 0;
  local coordsY = 0;
  local facing = 0;
local coordsXHex = nullReference;
local coordsYHex = nullReference;

coordsX, coordsY = GetPlayerMapPosition("player");
coordsX = ceil((coordsX * 10000) - .5)
coordsY = ceil((coordsY * 10000) - .5)
coordsXHex = padOutHex(string.format("%02x",coordsX))
coordsYHex = padOutHex(string.format("%02x",coordsY))
facing =  padOutHex(string.format("%02x",ceil((facingAngle() * 100) - .5)))
	
 if (GetZoneText() == "Darnassus" ) then
  inZone = "|cFFFAFAFA!";
 end

 if (GetZoneText() == "Stormwind City" ) then
  inZone = "|cFFFAFAAA!";
 end

 if (GetZoneText() == "IronForge" ) then
  inZone = "|cFFFAFABB!";
 end

 if (GetZoneText() == "Shattrath City" ) then
  inZone = "|cFFFAFACC!";
 end
 
 if (GetZoneText() == "Silvermoon City" ) then
  inZone = "|cFFFAFADD!";
 end
 
 if (IsOutdoors() == 1) then
   isOutDoors = "|cFF808080!"; 
   else
   isOutDoors = "|cFF666768!"; --Implied if your not outdoors your what then
  end
  
 if (UnitIsDeadOrGhost("player")) then
  isDead = "|cFF010203!";
 end

 if (GetZoneText() == "Eye of the Storm" ) then
  inZone = "|cFFF5F5F5!";
  subzoneText = GetSubZoneText();
  if (subzoneText == "Mage Tower") then
   inSubZone = "|cFFC1C2C3!";
  end
  if (subzoneText == "Draenei Ruins") then
   inSubZone = "|cFFC2C3C4!"; 
  end
  if (subzoneText == "Blood Elf Tower") then
  inSubZone = "|cFFC3C4C5!"; 
  end
  if (subzoneText == "Fel Reaver Ruins") then
  inSubZone = "|cFFC4C5C6!"; 
  end
  
 end

 if (GetZoneText() == "Warsong Gulch" ) then
  inZone = "|cFFA1A2A3!";
  subzoneText = GetSubZoneText();
  if (subzoneText == "Silverwing Hold") then
   inSubZone = "|cFFA1A3A4!";
  end
    if (subzoneText == "Warsong Lumber Mill") then
   inSubZone = "|cFFA1A3A4!";
  end
 end
 
 if (GetZoneText() == "Arathi Basin" ) then
  inZone = "|cFFB1B2B3!";
  subzoneText = GetSubZoneText();
  if (subzoneText == "Trollbane Hall") then
   inSubZone = "|cFFB2B3B4!";
  end
  if (subzoneText == "Stables") then
   inSubZone = "|cFFB3B4B5!";
  end
  if (subzoneText == "Gold Mine") then
   inSubZone = "|cFFB4B5B6!";
  end
  if (subzoneText == "Farm") then
   inSubZone = "|cFFB5B6B7!";
  end
  if (subzoneText == "Lumber Mill") then
   inSubZone = "|cFFB6B7B8!";
  end
  if (subzoneText == "Blacksmith") then
   inSubZone = "|cFFB7B8B9!";
  end
  if (subzoneText == "Defiler's Den") then
   inSubZone = "|cFFB7B9B9!";
  end
 end

  if (GetZoneText() == "Alterac Valley" ) then
    inZone = "|cFFF0F1F2!";
    subzoneText = GetSubZoneText();
  if (subzoneText == "Dun Baldar Pass") then
   inSubZone = "|cFFF1F2F3!";
  end
  if (subzoneText == "Stormpike Graveyard") then
   inSubZone = "|cFFF2F3F4!";
  end
  if (subzoneText == "Dun Baldar") then
   inSubZone = "|cFFF3F4F5!";
  end
  if (subzoneText == "Winterax Hold") then
   inSubZone = "|cFFF4F5F6!";
  end
  if (subzoneText == "Iceblood Graveyard") then
   inSubZone = "|cFFF5F6F7!";
  end
  if (subzoneText == "Stonehearth Graveyard") then
   inSubZone = "|cFFF6F7F8!";
  end
  if (subzoneText == "Frostwolf Graveyard") then
   inSubZone = "|cFFF7F8F9!";
  end
  if (subzoneText == "Frostwolf Relif Hut") then
   inSubZone = "|cFFF8F9FA!";
  end


 end

  
  
  if (GetNumRaidMembers() > 2) then
   inBattleGround = "|cFFFFFFFF!"; 
  end
  
 --In queue is in order ,  could be any number 1 - 4 since there are 4 battle grounds and you can queue for all at once.
 if (GetBattlefieldEstimatedWaitTime(0) > 0) then
  inQueue = "|cFFD1D2D3!";
 end

 if (GetBattlefieldEstimatedWaitTime(1) > 0) then --AB
  inQueue = "|cFFD1D2D3!";
 end

 if (GetBattlefieldEstimatedWaitTime(2) > 0) then
  inQueue = "|cFFD1D2D3!";
 end
if (GetBattlefieldEstimatedWaitTime(3) > 0) then
  inQueue = "|cFFD1D2D3!";
 end
if (GetBattlefieldEstimatedWaitTime(4) > 0) then
  inQueue = "|cFFD1D2D3!";
 end
if (GetBattlefieldEstimatedWaitTime(5) > 0) then
  inQueue = "|cFFD1D2D3!";
 end
if (GetBattlefieldEstimatedWaitTime(6) > 0) then
  inQueue = "|cFFD1D2D3!";
 end
 
 
 
ccStartMarker:SetTexture(decToFloat(tonumber(string.sub(ReferenceMark,5,6),16)), decToFloat(tonumber(string.sub(ReferenceMark,7,8),16)), decToFloat(tonumber(string.sub(ReferenceMark,9,10),16)) ); 
ccinBattleGround:SetTexture(decToFloat(tonumber(string.sub(inBattleGround,5,6),16)), decToFloat(tonumber(string.sub(inBattleGround,7,8),16)), decToFloat(tonumber(string.sub(inBattleGround,9,10),16)) ); 
ccinZone:SetTexture(decToFloat(tonumber(string.sub(inZone,5,6),16)), decToFloat(tonumber(string.sub(inZone,7,8),16)), decToFloat(tonumber(string.sub(inZone,9,10),16)) ); 
ccisOutDoors:SetTexture(decToFloat(tonumber(string.sub(isOutDoors,5,6),16)), decToFloat(tonumber(string.sub(isOutDoors,7,8),16)), decToFloat(tonumber(string.sub(isOutDoors,9,10),16)) ); 
ccisDead:SetTexture(decToFloat(tonumber(string.sub(isDead,5,6),16)), decToFloat(tonumber(string.sub(isDead,7,8),16)), decToFloat(tonumber(string.sub(isDead,9,10),16)) ); 
ccinSubZone:SetTexture(decToFloat(tonumber(string.sub(inSubZone,5,6),16)), decToFloat(tonumber(string.sub(inSubZone,7,8),16)), decToFloat(tonumber(string.sub(inSubZone,9,10),16)) ); 
ccinQueue:SetTexture(decToFloat(tonumber(string.sub(inQueue,5,6),16)), decToFloat(tonumber(string.sub(inQueue,7,8),16)), decToFloat(tonumber(string.sub(inQueue,9,10),16)) ); 
ccReferenceMark2:SetTexture(decToFloat(tonumber(string.sub(ReferenceMark2,5,6),16)), decToFloat(tonumber(string.sub(ReferenceMark2,7,8),16)), decToFloat(tonumber(string.sub(ReferenceMark2,9,10),16)) ); 

 cccoordsXHex:SetTexture(decToFloat(tonumber(string.sub(coordsXHex,5,6),16)), decToFloat(tonumber(string.sub(coordsXHex,7,8),16)), decToFloat(tonumber(string.sub(coordsXHex,9,10),16)) ); 
 ccReferenceMark3:SetTexture(decToFloat(tonumber(string.sub(ReferenceMark3,5,6),16)), decToFloat(tonumber(string.sub(ReferenceMark3,7,8),16)), decToFloat(tonumber(string.sub(ReferenceMark3,9,10),16)) ); 
  cccoordsYHex:SetTexture(decToFloat(tonumber(string.sub(coordsYHex,5,6),16)), decToFloat(tonumber(string.sub(coordsYHex,7,8),16)), decToFloat(tonumber(string.sub(coordsYHex,9,10),16)) ); 
ccReferenceMark4:SetTexture(decToFloat(tonumber(string.sub(ReferenceMark4,5,6),16)), decToFloat(tonumber(string.sub(ReferenceMark4,7,8),16)), decToFloat(tonumber(string.sub(ReferenceMark4,9,10),16)) ); 
ccFacing:SetTexture(decToFloat(tonumber(string.sub(facing,5,6),16)), decToFloat(tonumber(string.sub(facing,7,8),16)), decToFloat(tonumber(string.sub(facing,9,10),16)) ); 
  ccMarkEnd:SetTexture(decToFloat(tonumber(string.sub(MarkEnd,5,6),16)), decToFloat(tonumber(string.sub(MarkEnd,7,8),16)), decToFloat(tonumber(string.sub(MarkEnd,9,10),16)) ); 

 --cccoordsYHex:SetTexture(255,0,0);
--cccoordsYHex:SetTexture(128,128,128); 
 --return coordsXHex;
   --return ""--string.sub(coordsYHex,7,8) .. string.format("%02x",coordsY); 
   -- return ReferenceMark  .. inBattleGround .. inZone .. isOutDoors .. isDead .. inSubZone .. inQueue .. ReferenceMark2..coordsXHex .. ReferenceMark3.. coordsYHex;
--return padOutHex(string.format("%02x",coordsX));
return "";
--return padOutHex(coordsXHex);
end
function CustomClock:Main_UpdateDisplay()
	
	--self.DISPLAY_FRAME.TEXT:SetText(date(self.Config.TimePattern));
	--self.DISPLAY_FRAME.TEXT:FontString:SetFont("FRIZQT__.TTF",11,"OUTLINE, MONOCHROME")

	self.DISPLAY_FRAME.TEXT:SetText(self.GetPosInfo());


	
	

end




function CustomClock:Main_OnEvent(event)
	if (event == "VARIABLES_LOADED") then
		self:Config_LoadSettings();
	elseif (event == "PLAYER_LOGOUT") then
		self:Config_SaveSettings();
	
	end;
end


CustomClock.SCRIPTS_FRAME:SetScript("OnEvent", (function(self, event, ...) self.CUSTOMCLOCK:Main_OnEvent(event); end));




CustomClock.SCRIPTS_FRAME:SetScript("OnUpdate",
  function(self, elapsed)
	self.Elapsed = (self.Elapsed + elapsed);
	
	if (self.Elapsed > 0.01) then
		self.Elapsed = 0.0;
		if AUTOBGR_BG_NAMES[ GetZoneText() ] then
		  if ( (GetReleaseTimeRemaining() < 330) and (UnitIsDeadOrGhost("player"))) then
		    RepopMe();
		  end
	    end
		self.CUSTOMCLOCK:Main_UpdateDisplay();
	end
  end
);




function CustomClock:Main_SlashCommandHandler(msg)
	local command, argument = msg:match("([^%s]+) (.+)");
	command = (command or msg):lower();
	
	local localization = self.LOCALIZATION;
	
	
	if (command == localization.SLASH_PATTERN) then
		if (argument ~= nil and argument ~= "") then
			local dateString = self:Config_SetDisplayPattern(argument);
			if (dateString ~= nil) then
				DEFAULT_CHAT_FRAME:AddMessage(localization.NEW_PATTERN:format(argument), 0.6, 0.8, 0.6);
			else
				DEFAULT_CHAT_FRAME:AddMessage(localization.INVALID_PATTERN, 0.6, 0.8, 0.6);
			end
		else
			local chatFrame = DEFAULT_CHAT_FRAME;
			local addMessage = chatFrame.AddMessage;
			for index = 1, 23 do
				addMessage(chatFrame, localization["PATTERN_HELP_"..index], 0.6, 0.8, 0.6);
			end
			addMessage(chatFrame, localization.CURRENT_PATTERN:format(self.Config.TimePattern), 0.6, 0.8, 0.6);
		end
		
		
	elseif (command == localization.SLASH_COLOR) then
		local tonumber = tonumber;
		local mathceil = math.ceil;
		
		local redVal, greenVal, blueVal = (argument or ""):match("(%d+) (%d+) (%d+)");
		
		if (redVal ~= nil) then
			local success = self:Config_SetTextColor(mathceil(tonumber(redVal)), mathceil(tonumber(greenVal)), mathceil(tonumber(blueVal)));
			
			if (success == true) then
				DEFAULT_CHAT_FRAME:AddMessage(localization.NEW_COLORS:format(redVal, greenVal, blueVal), 0.6, 0.8, 0.6);
			else
				DEFAULT_CHAT_FRAME:AddMessage(localization.INVALID_COLORS, 0.6, 0.8, 0.6);
			end
		else
			DEFAULT_CHAT_FRAME:AddMessage(localization.INVALID_COLORS, 0.6, 0.8, 0.6);
		end
		
		
	elseif (command == localization.SLASH_FONTPATH) then
		if ((argument or ""):lower() == localization.SLASH_FONTPATH_LIST) then
			DEFAULT_CHAT_FRAME:AddMessage(localization.FONT_LIST, 0.6, 0.8, 0.6);
		else
			local success = self:Config_SetFontPath(argument);
			if (success == true) then
				DEFAULT_CHAT_FRAME:AddMessage(localization.NEW_FONT:format(argument), 0.6, 0.8, 0.6);
			else
				DEFAULT_CHAT_FRAME:AddMessage(localization.INVALID_FONT, 0.6, 0.8, 0.6);
			end
		end
		
		
	elseif (command == localization.SLASH_SIZE) then
		local success = self:Config_SetFontSize(argument);
		if (success == true) then
			DEFAULT_CHAT_FRAME:AddMessage(localization.NEW_SIZE:format(argument), 0.6, 0.8, 0.6);
		else
			DEFAULT_CHAT_FRAME:AddMessage(localization.INVALID_SIZE, 0.6, 0.8, 0.6);
		end
		
		
	elseif (command == localization.SLASH_WIDTH) then	
		local success = self:Config_SetFrameWidth(argument);
		if (success == true) then
			DEFAULT_CHAT_FRAME:AddMessage(localization.NEW_WIDTH:format(argument), 0.6, 0.8, 0.6);
		else
			DEFAULT_CHAT_FRAME:AddMessage(localization.INVALID_WIDTH, 0.6, 0.8, 0.6);
		end
		
		
	elseif (command == localization.SLASH_HEIGHT) then	
		local success = self:Config_SetFrameHeight(argument);
		if (success == true) then
			DEFAULT_CHAT_FRAME:AddMessage(localization.NEW_HEIGHT:format(argument), 0.6, 0.8, 0.6);
		else
			DEFAULT_CHAT_FRAME:AddMessage(localization.INVALID_HEIGHT, 0.6, 0.8, 0.6);
		end
		
		
	else
		local chatFrame = DEFAULT_CHAT_FRAME;
		local addMessage = chatFrame.AddMessage;
		
		for index = 1, 7 do
			addMessage(chatFrame, localization["COMMANDS_HELP_"..index], 0.6, 0.8, 0.6);
		end
	end
end


SlashCmdList["CUSTOMCLOCK"] = (function(msg) CustomClock:Main_SlashCommandHandler(msg); end);
SLASH_CUSTOMCLOCK1 = ("/cucl");

